package com.huike.stock_analysis.service;

import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.stock_analysis.dao.FinanceDao;

import com.huike.stock_analysis.entity.OperationInfo;

@Service
public class OperationService {
	//注入dao
	@Autowired
	private FinanceDao financeDao;
	//保留四位小数
	private DecimalFormat df = new DecimalFormat("0.0000");
	
	//保存数据库查询到的数据
	private List<OperationInfo> operationInfos;

	
	private void setList(String code) {
		
			operationInfos = financeDao.queryOperation(code);
		
	}
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  存货周转天数
	 */
	public Map<String, String> inventoryTurnoverDays(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		//查询数据库
		setList(code);
		
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			System.out.println(operationInfo.getInventoryTurnoverRatio());
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getInventoryTurnoverRatio()) + "," + df.format(operationInfo.getInventoryTurnoverRatioOfSame()));
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  总资产周转率
	 */
	public Map<String, String> totalAssetsTurnoverRatio(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		setList(code);
		
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getTotalAssetsTurnoverRatio()) + ',' + df.format(operationInfo.getTotalAssetsTurnoverRatioOfSame()));
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  流动资金周转率
	 */
	public Map<String, String> flowCapitalTurnoverRatio(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		setList(code);
		
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getFlowCapitalTurnoverRatio()));
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  应收账款周转率
	 */
	public Map<String, String> accountsReceivableTurnoverRatio(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		setList(code);
		
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getAccountsReceivableTurnoverRatio()));
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  固定资金周转率
	 */
	public Map<String, String> fixCapitalTurnoverRatio(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		setList(code);
		
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getFixCapitalTurnoverRatio()));
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
	 *  资产现金回收率
	 */
	public Map<String, String> cashRatio(String code) {
		//创建返回map
		Map<String, String> map = new LinkedHashMap<>();
		
		
		setList(code);
		//遍历查询结果
		for(OperationInfo operationInfo : operationInfos) {
			
			map.put(operationInfo.getDate().toString(), df.format(operationInfo.getCashRatio()));
		}
		
		return map;
	}

}
